using System;
using DZTC.UselessAppEver.Core;
using NUnit.Framework;

namespace DZTC.UselessAppEver.Tests
{
    [TestFixture]
    internal class ProgressTests
    {
        [Test]
        public void BasicTest()
        {
            var pt = new ProgressToken();
            pt.Increment(10);

            Assert.AreEqual(pt.Value, 10);
        }


        [Test]
        public void TestFailure()
        {
            var pt = new ProgressToken();
            pt.Increment(100);

            Assert.Throws<ArgumentException>(() => pt.Increment(1));
        }


        [Test]
        public void TestFork()
        {
            var pt = new ProgressToken();

            using (pt.Fork(0.1))
            {
                pt.Increment(100);
            }

            Assert.AreEqual(pt.Value, 10);

            pt.Increment(10);
            Assert.AreEqual(pt.Value, 20);
        }


        [Test]
        public void TestReport()
        {
            var pt = new ProgressToken();

            pt.Report(10);
            Assert.AreEqual(pt.Value, 10);

            using (pt.Fork(0.1))
            {
                pt.Report(50);
            }

            Assert.AreEqual(pt.Value, 15);
        }

        [Test]
        public void TestSubscription()
        {
            var pt = new ProgressToken();

            var calls = 0;
            pt.ProgressChanged += (value, delta) =>
                {
                    calls++;
                };


            using (pt.Fork(0.01))
            {
                pt.Increment(90);
                pt.Increment(90);
                pt.Increment(90);
                pt.Increment(90);
            }

            Assert.AreEqual(3, calls);
        }

    }
}